package main

import (
	"bufio"
	"flag"
	"fmt"
	"net"
	"os"
)

var (
	addr string
	port int
)

func main() {
	flag.StringVar(&addr, "addr", "", "server ip address")
	flag.IntVar(&port, "p", 0, "server port")
	flag.Parse()

	if addr == "" || port == 0 {
		fmt.Printf("input params error addr:[%s] port:[%d]\n", addr, port)
	}

	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", addr, port))
	defer conn.Close()
	if err != nil {
		fmt.Printf("connected server [%s] faild err:%v\n", addr, err)
		return
	}
	input := bufio.NewReader(os.Stdin)
	for {
		bytes, _, err := input.ReadLine()
		if err != nil {
			fmt.Printf("read line faild err:%v\n", err)
		}
		str := string(bytes)
		if str == "Q" || str == "q" {
			fmt.Println("exe quit!")
			break
		}
		n, err := conn.Write(bytes)
		if err != nil {
			fmt.Printf("send data faild err:%v\n", err)
		} else {
			fmt.Printf("send data length %d\n", n)
		}
	}

}
